package cn.com.aiton.trafficuser.service.impl;

import cn.com.aiton.common.model.ServiceResult;
import cn.com.aiton.common.model.UserDomain;
import cn.com.aiton.common.utils.DateTimeUtil;
import cn.com.aiton.common.utils.ListBeanUtils;
import cn.com.aiton.trafficuser.entity.UserEntity;
import cn.com.aiton.trafficuser.repository.TrafficUserRepository;
import cn.com.aiton.trafficuser.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    private static final Logger LOG = LoggerFactory.getLogger(UserService.class);
    @Autowired
    private TrafficUserRepository repository;

    @Override
    public ServiceResult<UserDomain> findByUsername(String username) {
        ServiceResult<UserDomain> domainServiceResult = new ServiceResult<>();
        UserDomain domain = new UserDomain();
        try{
            UserEntity entity = repository.findByUsername(username);
            BeanUtils.copyProperties(entity,domain);
            domainServiceResult.setObject(domain);
            domainServiceResult.setSuccess(true);
            domainServiceResult.setMessage("success");
        }catch (Exception e){
            domainServiceResult.setObject(null);
            domainServiceResult.setSuccess(false);
            domainServiceResult.setMessage("failure");
        }
        return domainServiceResult;
    }

    /**
     * 保存开道车数据到数据库
     *
     * @param username
     * @param password
     * @param type
     * @param phone
     * @param plate
     * @param times
     * @return
     */
    @Override
    public ServiceResult<UserDomain> save(String username, String password, Integer type, String phone, String plate, Date times) {
        ServiceResult<UserDomain> result = new ServiceResult<>();
        try {
            UserEntity entity = new UserEntity(username, password, type, phone, times);
            UserEntity entity1 = repository.save(entity);
            UserDomain domain = new UserDomain();
            BeanUtils.copyProperties(entity1, domain);
            result.setObject(domain);
            result.setMessage("成功");
            result.setSuccess(true);
            return result;
        }catch (Exception e){
            result.setObject(null);
            result.setMessage("失败");
            result.setSuccess(false);
            return result;
        }
    }

    /**
     * 根据ID查找开道车
     *
     * @param id
     * @return
     */
    @Override
    public ServiceResult<UserDomain> findById(Long id) {
        ServiceResult<UserDomain> result = new ServiceResult<>();
        try {
            UserEntity entity1 = repository.findById(id).get();
            UserDomain domain = new UserDomain();
            BeanUtils.copyProperties(entity1, domain);
            result.setObject(domain);
            result.setMessage("成功");
            result.setSuccess(true);
            return result;
        }catch (Exception e){
            result.setObject(null);
            result.setMessage("失败");
            result.setSuccess(false);
            return result;
        }
    }

    /**
     * 查询所有开道车
     *arcgis
     * @return
     */
    @Override
    public ServiceResult<List<UserDomain>> findAll() {
        ServiceResult<List<UserDomain>> result = new ServiceResult<>();
        try {
            List<UserEntity> entity1 = repository.findAll();
            List<UserDomain> domains = new ArrayList<>();
            for (UserEntity en :
                    entity1) {
                UserDomain userDomain = new UserDomain();
                userDomain.setPassword(en.getPassword());
                userDomain.setPhone(en.getPhone());
                userDomain.setStime(DateTimeUtil.date2String(en.getTimes()));
                userDomain.setUsername(en.getUsername());
                userDomain.setType(en.getType());
                userDomain.setId(en.getId());
                domains.add(userDomain);
            }
            result.setObject(domains);
            result.setMessage("成功");
            result.setSuccess(true);
            return result;
        }catch (Exception e){
            result.setObject(null);
            result.setMessage("失败");
            result.setSuccess(false);
            return result;
        }
    }

    /**
     * 更新开道车数据到数据库
     *
     * @param id
     * @param username
     * @param password
     * @param type
     * @param phone
     * @param plate
     * @param times
     * @return
     */
    @Override
    public ServiceResult<UserDomain> update(Long id, String username, String password, Integer type, String phone, String plate, Date times) {
        ServiceResult<UserDomain> result = new ServiceResult<>();
        UserDomain domain = new UserDomain();
        try {
            UserEntity entity1 = repository.findById(id).get();
            entity1.setUsername(username);
            entity1.setPassword(password);
            entity1.setType(type);
            entity1.setPhone(phone);
            entity1.setTimes(times);
            repository.saveAndFlush(entity1);
            BeanUtils.copyProperties(entity1, domain);
            result.setObject(domain);
            result.setMessage("成功");
            result.setSuccess(true);
            return result;
        }catch (Exception e){
            result.setObject(null);
            result.setMessage("失败");
            result.setSuccess(false);
            return result;
        }
    }

    /**
     * 根据ID删除开道车数据
     *
     * @param id
     * @return
     */
    @Override
    public ServiceResult<UserDomain> delete(Long id) {
        ServiceResult<UserDomain> result = new ServiceResult<>();
        UserDomain domain = new UserDomain();
        try {
            UserEntity entity1 = repository.findById(id).get();
            repository.delete(entity1);
            BeanUtils.copyProperties(entity1, domain);
            result.setObject(domain);
            result.setMessage("成功");
            result.setSuccess(true);
            return result;
        }catch (Exception e){
            result.setObject(null);
            result.setMessage("失败");
            result.setSuccess(false);
            return result;
        }
    }

    /**
     * 根据ID删除开道车数据
     *
     * @param name
     * @param password
     * @return
     */
    @Override
    public ServiceResult<UserDomain> login(String name, String password) {
        ServiceResult<UserDomain> result = new ServiceResult<>();
        try {
            UserEntity entity1 = repository.findByUsername(name);
            boolean passwd = BCrypt.checkpw(password,entity1.getPassword());
            if (passwd){
                UserDomain domain = new UserDomain();
                domain.setType(entity1.getType());
                domain.setUsername(entity1.getUsername());
                try{

                    domain.setStime(DateTimeUtil.date2String(entity1.getTimes()));
                }catch (Exception e){

                    domain.setStime(String.valueOf(new Date()));
                }
                domain.setPhone(entity1.getPhone());
                domain.setId(entity1.getId());
                domain.setPassword("No");
                result.setObject(domain);
                result.setMessage("成功");
                result.setSuccess(true);
                return result;
            }else {

                result.setObject(null);
                result.setMessage("密码错误");
                result.setSuccess(false);
                return result;
            }
        }catch (Exception e){
            result.setObject(null);
            result.setMessage("系统异常");
            result.setSuccess(false);
            return result;
        }
    }
}
